//
//  ActionSheet.m
//  LotteryClient
//
//  Created by Sunyon 14-4-11.
//  Copyright (c) 2014年 CTTC. All rights reserved.
//

#import "ActionSheet.h"

@interface ActionSheet ()
{
	UIActionSheet *_sheet;
	NSMutableArray *_blocks;
}
@end

@implementation ActionSheet
- (id) initWithTitle: (NSString *) title {
    if ((self = [super init]) == nil)
        return nil;

    /* Initialize the sheet */
    _sheet = [[UIActionSheet alloc] initWithTitle: title delegate: self cancelButtonTitle: nil destructiveButtonTitle: nil otherButtonTitles: nil];

    /* Initialize button -> block array */
    _blocks = [[NSMutableArray alloc] init];

    return self;
}

- (void) dealloc
{
    _sheet.delegate = nil;
}


- (void) setCancelButtonWithTitle: (NSString *) title block: (void (^)()) block {
    [self addButtonWithTitle: title block: block];
    _sheet.cancelButtonIndex = _sheet.numberOfButtons - 1;
}

- (void) addButtonWithTitle: (NSString *) title block: (void (^)()) block {
    [_blocks addObject:[block copy]];
    [_sheet addButtonWithTitle: title];
}

- (void) showInView: (UIView *) view {
    [_sheet showInView: view];

    /* Ensure that the delegate (that's us) survives until the sheet is dismissed */
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    /* Run the button's block */
    if (buttonIndex >= 0 && buttonIndex < [_blocks count]) {
        void (^b)() = [_blocks objectAtIndex: buttonIndex];
        b();
    }

    /* Sheet to be dismissed, drop our self reference */
}


@end


#define kColumCountPerRow 3

#define WINDOW_COLOR                            [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2]
#define ACTIONSHEET_BACKGROUNDCOLOR             [UIColor colorWithRed:106/255.00f green:106/255.00f blue:106/255.00f alpha:0.8]
#define ANIMATE_DURATION                        0.25f

#define CORNER_RADIUS                           5
#define SHAREBUTTON_BORDER_WIDTH                0.5f
#define SHAREBUTTON_BORDER_COLOR                [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.8].CGColor
#define SHAREBUTTONTITLE_FONT                   [UIFont fontWithName:@"HelveticaNeue-Bold" size:18]

#define CANCEL_BUTTON_COLOR                     [UIColor colorWithRed:53/255.00f green:53/255.00f blue:53/255.00f alpha:1]

#define SHAREBUTTON_WIDTH                       50
#define SHAREBUTTON_HEIGHT                      50
#define SHAREBUTTON_INTERVAL_WIDTH               (([UIScreen mainScreen].applicationFrame.size.width - kColumCountPerRow*SHAREBUTTON_WIDTH)/(kColumCountPerRow + 1))
#define SHAREBUTTON_INTERVAL_HEIGHT             35

#define SHARETITLE_WIDTH                        50
#define SHARETITLE_HEIGHT                       20
#define SHARETITLE_INTERVAL_WIDTH               SHAREBUTTON_INTERVAL_WIDTH
#define SHARETITLE_INTERVAL_HEIGHT              SHAREBUTTON_WIDTH+SHAREBUTTON_INTERVAL_HEIGHT
#define SHARETITLE_FONT                         [UIFont fontWithName:@"Helvetica-Bold" size:14]

#define TITLE_INTERVAL_HEIGHT                   15
#define TITLE_HEIGHT                            35
#define TITLE_INTERVAL_WIDTH                    35
#define TITLE_WIDTH                             ([UIScreen mainScreen].applicationFrame.size.width - 2*TITLE_INTERVAL_WIDTH)
#define TITLE_FONT                              [UIFont fontWithName:@"Helvetica-Bold" size:10]
#define SHADOW_OFFSET                           CGSizeMake(0, 0.8f)
#define TITLE_NUMBER_LINES                      2

#define BUTTON_INTERVAL_HEIGHT                  20
#define BUTTON_HEIGHT                           40
#define BUTTON_INTERVAL_WIDTH                   40
#define BUTTON_WIDTH                            ([UIScreen mainScreen].applicationFrame.size.width - 2*BUTTON_INTERVAL_WIDTH)
#define BUTTONTITLE_FONT                        [UIFont fontWithName:@"HelveticaNeue-Bold" size:18]
#define BUTTON_BORDER_WIDTH                     0.5f
#define BUTTON_BORDER_COLOR                     [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.8].CGColor


@interface UIImage (custom)

+ (UIImage *)imageWithColor:(UIColor *)color;

@end


@implementation UIImage (custom)

+ (UIImage *)imageWithColor:(UIColor *)color
{
	CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
	UIGraphicsBeginImageContext(rect.size);
	CGContextRef context = UIGraphicsGetCurrentContext();
	
	CGContextSetFillColorWithColor(context, [color CGColor]);
	CGContextFillRect(context, rect);
	
	UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();
	
	return image;
}
@end

@interface IconActionSheet ()
@property(nonatomic,strong) UILabel *titleLabel;

@property (nonatomic,strong) UIView *backGroundView;
@property (nonatomic,strong) NSString *actionTitle;
@property (nonatomic,assign) NSInteger postionIndexNumber;
@property (nonatomic,assign) BOOL isHadTitle;
@property (nonatomic,assign) BOOL isHadShareButton;
@property (nonatomic,assign) BOOL isHadCancelButton;
@property (nonatomic,assign) CGFloat LXActivityHeight;
@property (nonatomic,assign) id<IconActionSheetDelegate>delegate;

@end

@implementation IconActionSheet

- (id)initWithFrame:(CGRect)frame
{
	self = [super initWithFrame:frame];
	if (self) {
		// Initialization code
	}
	return self;
}

#pragma mark - Public method

- (id)initWithTitle:(NSString *)title
		   delegate:(id<IconActionSheetDelegate>)delegate
  cancelButtonTitle:(NSString *)cancelButtonTitle 
  ShareButtonTitles:(NSArray *)shareButtonTitlesArray 
withShareButtonImagesName:(NSArray *)shareButtonImagesNameArray;
{
	self = [super init];
	if (self) {
		//初始化背景视图，添加手势
		self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
		self.backgroundColor = WINDOW_COLOR;
		self.userInteractionEnabled = YES;
		UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedCancel)];
		[self addGestureRecognizer:tapGesture];
		
		if (delegate) {
			self.delegate = delegate;
		}
		
		[self creatButtonsWithTitle:title cancelButtonTitle:cancelButtonTitle shareButtonTitles:shareButtonTitlesArray withShareButtonImagesName:shareButtonImagesNameArray];
		
	}
	return self;
}

- (void)showInView:(UIView *)view
{

    [view addSubview:self];
}

#pragma mark - Praviate method

- (void)creatButtonsWithTitle:(NSString *)title 
			cancelButtonTitle:(NSString *)cancelButtonTitle 
			shareButtonTitles:(NSArray *)shareButtonTitlesArray 
	withShareButtonImagesName:(NSArray *)shareButtonImagesNameArray
{
	//初始化
	self.isHadTitle = NO;
	self.isHadShareButton = NO;
	self.isHadCancelButton = NO;
	
	//初始化LXACtionView的高度为0
	self.LXActivityHeight = 0;
	
	//初始化IndexNumber为0;
	self.postionIndexNumber = 0;
	
	//生成LXActionSheetView
	self.backGroundView = [[UIView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width, 0)];
	self.backGroundView.backgroundColor = ACTIONSHEET_BACKGROUNDCOLOR;
	
	//给LXActionSheetView添加响应事件
	UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedBackGroundView)];
	[self.backGroundView addGestureRecognizer:tapGesture];
	
	[self addSubview:self.backGroundView];
	
	if (title) 
	{
		self.isHadTitle = YES;
		
		UILabel *titlelabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0,
																		[UIScreen mainScreen].applicationFrame.size.width, 
																		TITLE_HEIGHT)];
		titlelabel.backgroundColor = [UIColor clearColor];
		titlelabel.textAlignment = NSTextAlignmentCenter;
		titlelabel.shadowColor = [UIColor blackColor];
		titlelabel.shadowOffset = SHADOW_OFFSET;
		titlelabel.font = SHARETITLE_FONT;
		titlelabel.text = title;
		titlelabel.textColor = [UIColor whiteColor];
		titlelabel.numberOfLines = TITLE_NUMBER_LINES;
		
		self.titleLabel = titlelabel;
		self.LXActivityHeight = self.LXActivityHeight + 2*TITLE_INTERVAL_HEIGHT+TITLE_HEIGHT;
		[self.backGroundView addSubview:self.titleLabel];
	}
	
	if (shareButtonImagesNameArray) 
	{
		if (shareButtonImagesNameArray.count > 0) 
		{
			self.isHadShareButton = YES;
			for (int i = 1; i < shareButtonImagesNameArray.count+1; i++) {
				//计算出行数，与列数
				int column = (int)ceil((float)(i)/kColumCountPerRow); //行
				int line = (i)%kColumCountPerRow; //列
				if (line == 0) {
					line = 3;
				}
				UIButton *shareButton = [self creatShareButtonWithColumn:column andLine:line];
				shareButton.tag = self.postionIndexNumber;
				[shareButton addTarget:self action:@selector(didClickOnImageIndex:) forControlEvents:UIControlEventTouchUpInside];
				
				[shareButton setBackgroundImage:[UIImage imageNamed:[shareButtonImagesNameArray objectAtIndex:i-1]] forState:UIControlStateNormal];
				//有Title的时候
				if (self.isHadTitle == YES) 
				{
					[shareButton setFrame:CGRectMake(SHAREBUTTON_INTERVAL_WIDTH+((line-1)*(SHAREBUTTON_INTERVAL_WIDTH+SHAREBUTTON_WIDTH)),
													 self.LXActivityHeight+((column-1)*(SHAREBUTTON_INTERVAL_HEIGHT+SHAREBUTTON_HEIGHT)),
													 SHAREBUTTON_WIDTH, 
													 SHAREBUTTON_HEIGHT)];
				}
				else
				{
					[shareButton setFrame:CGRectMake(SHAREBUTTON_INTERVAL_WIDTH+((line-1)*(SHAREBUTTON_INTERVAL_WIDTH+SHAREBUTTON_WIDTH)),
													 SHAREBUTTON_INTERVAL_HEIGHT+((column-1)*(SHAREBUTTON_INTERVAL_HEIGHT+SHAREBUTTON_HEIGHT)),
													 SHAREBUTTON_WIDTH,
													 SHAREBUTTON_HEIGHT)];
				}
				[self.backGroundView addSubview:shareButton];
				
				self.postionIndexNumber++;
			}
		}
	}
	
	if (shareButtonTitlesArray) {
		if (shareButtonTitlesArray.count > 0 && shareButtonImagesNameArray.count > 0) {
			for (int j = 1; j < shareButtonTitlesArray.count+1; j++) {
				//计算出行数，与列数
				int column = (int)ceil((float)(j)/3); //行
				int line = (j)%3; //列
				if (line == 0) {
					line = 3;
				}
				UILabel *shareLabel = [self creatShareLabelWithColumn:column andLine:line];
				shareLabel.text = [shareButtonTitlesArray objectAtIndex:j-1];
				//有Title的时候
				if (self.isHadTitle == YES) {
					[shareLabel setFrame:CGRectMake(SHARETITLE_INTERVAL_WIDTH+((line-1)*(SHARETITLE_INTERVAL_WIDTH+SHARETITLE_WIDTH)), self.LXActivityHeight+SHAREBUTTON_HEIGHT+((column-1)*(SHARETITLE_INTERVAL_HEIGHT)), SHARETITLE_WIDTH, SHARETITLE_HEIGHT)];
				}
				[self.backGroundView addSubview:shareLabel];
			}
		}
	}
	
	//再次计算加入shareButtons后LXActivity的高度
	if (shareButtonImagesNameArray && shareButtonImagesNameArray.count > 0) {
		int totalColumns = (int)ceil((float)(shareButtonImagesNameArray.count)/3);
		if (self.isHadTitle  == YES) {
			self.LXActivityHeight = self.LXActivityHeight + totalColumns*(SHAREBUTTON_INTERVAL_HEIGHT+SHAREBUTTON_HEIGHT);
		}
		else{
			self.LXActivityHeight = SHAREBUTTON_INTERVAL_HEIGHT + totalColumns*(SHAREBUTTON_INTERVAL_HEIGHT+SHAREBUTTON_HEIGHT);
		}
	}
	
	if (cancelButtonTitle) {
		self.isHadCancelButton = YES;
		UIButton *cancelButton = [self creatCancelButtonWith:cancelButtonTitle];
		cancelButton.tag = self.postionIndexNumber;
		[cancelButton addTarget:self action:@selector(didClickOnImageIndex:) forControlEvents:UIControlEventTouchUpInside];
		
		//当没title destructionButton otherbuttons时
		if (self.isHadTitle == NO && self.isHadShareButton == NO) {
			self.LXActivityHeight = self.LXActivityHeight + cancelButton.frame.size.height+(2*BUTTON_INTERVAL_HEIGHT);
		}
		//当有title或destructionButton或otherbuttons时
		if (self.isHadTitle == YES || self.isHadShareButton == YES) {
			[cancelButton setFrame:CGRectMake(cancelButton.frame.origin.x, self.LXActivityHeight, cancelButton.frame.size.width, cancelButton.frame.size.height)];
			self.LXActivityHeight = self.LXActivityHeight + cancelButton.frame.size.height+BUTTON_INTERVAL_HEIGHT;
		}
		[self.backGroundView addSubview:cancelButton];
		
		self.postionIndexNumber++;
	}
	
	[UIView animateWithDuration:ANIMATE_DURATION animations:^{
		[self.backGroundView setFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height-self.LXActivityHeight, [UIScreen mainScreen].bounds.size.width, self.LXActivityHeight)];
	} completion:^(BOOL finished) {
	}];
}


- (UIButton *)creatCancelButtonWith:(NSString *)cancelButtonTitle
{
	UIButton *cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(BUTTON_INTERVAL_WIDTH,
																		BUTTON_INTERVAL_HEIGHT, 
																		BUTTON_WIDTH, 
																		BUTTON_HEIGHT)];
	cancelButton.layer.masksToBounds = YES;
	cancelButton.layer.cornerRadius = CORNER_RADIUS;
	
	cancelButton.layer.borderWidth = BUTTON_BORDER_WIDTH;
	cancelButton.layer.borderColor = BUTTON_BORDER_COLOR;
	
//	UIImage *image = [UIImage imageWithColor:CANCEL_BUTTON_COLOR];
//	[cancelButton setBackgroundImage:image forState:UIControlStateNormal];
	cancelButton.backgroundColor = [UIColor colorWithHexString:@"808080"];
	[cancelButton setTitle:cancelButtonTitle forState:UIControlStateNormal];
	cancelButton.titleLabel.font = BUTTONTITLE_FONT;
	[cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
	
	return cancelButton;
}

- (UIButton *)creatShareButtonWithColumn:(int)column andLine:(int)line
{
	UIButton *shareButton = [[UIButton alloc] initWithFrame:CGRectMake(SHAREBUTTON_INTERVAL_WIDTH+((line-1)*(SHAREBUTTON_INTERVAL_WIDTH+SHAREBUTTON_WIDTH)), SHAREBUTTON_INTERVAL_HEIGHT+((column-1)*(SHAREBUTTON_INTERVAL_HEIGHT+SHAREBUTTON_HEIGHT)), SHAREBUTTON_WIDTH, SHAREBUTTON_HEIGHT)];
	return shareButton;
}

- (UILabel *)creatShareLabelWithColumn:(int)column andLine:(int)line
{
	UILabel *shareLabel = [[UILabel alloc] initWithFrame:CGRectMake(SHARETITLE_INTERVAL_WIDTH+((line-1)*(SHARETITLE_INTERVAL_WIDTH+SHARETITLE_WIDTH)), SHARETITLE_INTERVAL_HEIGHT+((column-1)*(SHARETITLE_INTERVAL_HEIGHT)), SHARETITLE_WIDTH, SHARETITLE_HEIGHT)];
	
	shareLabel.backgroundColor = [UIColor clearColor];
	shareLabel.textAlignment = NSTextAlignmentCenter;
	shareLabel.font = TITLE_FONT;
	shareLabel.textColor = [UIColor colorWithHexString:@"494949"];
	return shareLabel;
}

- (UILabel *)creatTitleLabelWith:(NSString *)title
{
	
	UILabel *titlelabel = [[UILabel alloc] initWithFrame:CGRectMake(TITLE_INTERVAL_WIDTH, TITLE_INTERVAL_HEIGHT,
																	[UIScreen mainScreen].applicationFrame.size.width - 2*TITLE_INTERVAL_WIDTH, 
																	TITLE_HEIGHT)];
	titlelabel.backgroundColor = [UIColor clearColor];
	titlelabel.textAlignment = NSTextAlignmentCenter;
	titlelabel.shadowColor = [UIColor blackColor];
	titlelabel.shadowOffset = SHADOW_OFFSET;
	titlelabel.font = SHARETITLE_FONT;
	titlelabel.text = title;
	titlelabel.textColor = [UIColor whiteColor];
	titlelabel.numberOfLines = TITLE_NUMBER_LINES;
	return titlelabel;
}

- (void)didClickOnImageIndex:(UIButton *)button
{
	if (self.delegate) {
		if (button.tag != self.postionIndexNumber-1) {
			if ([self.delegate respondsToSelector:@selector(didClickOnImageIndex:)] == YES) {
				[self.delegate didClickOnImageIndex:(NSInteger *)button.tag];
			}
		}
		else{
			if ([self.delegate respondsToSelector:@selector(didClickOnCancelButton)] == YES){
				[self.delegate didClickOnCancelButton];
			}
		}
	}
	[self tappedCancel];
}

- (void)tappedCancel
{
	[UIView animateWithDuration:ANIMATE_DURATION animations:^{
		[self.backGroundView setFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width, 0)];
		self.alpha = 0;
	} completion:^(BOOL finished) {
		if (finished) {
			[self removeFromSuperview];
		}
	}];
}

- (void)tappedBackGroundView
{
	//
	
}




@end
